package main.java.com.yc.navy.snacknet.controller;

import java.io.IOException;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.yc.navy.snacknet.util.ConstantIfo;
import com.yc.navy.snacknet.util.RequestParamUtul;
import com.yc.navy.snacknet.util.SendEmailUtil;
import com.yc.navy.snacknet.vo.ResultVO;
import com.yc.navy.snacknet.bean.MemberInfo;
import com.yc.navy.snacknet.enums.ResultEnum;
import com.yc.navy.snacknet.service.IMemberInfoService;

@RestController
@RequestMapping("/menber")
public class MemberInfoController{
	
	@Autowired
	private SendEmailUtil sendEmailUtil;
	
	@Autowired
	private IMemberInfoService memberInfoService;
	
	/**
	 * 发送邮箱验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@PostMapping("/sendCode")
	public ResultVO sendCode(@RequestParam("email") String receive, String nickName, HttpSession session) throws IOException {
		String code = "";
		Random rd = new Random();
		while(code.length() < 6) {
			code += rd.nextInt(9);
		}
		
		if(sendEmailUtil.send(receive, code, nickName)) {
			session.setAttribute(ConstantIfo.CURRENTEMALLCODE, code);
			System.out.println(session.getAttribute(ConstantIfo.CURRENTEMALLCODE) +   code);
			TimerTask task = new TimerTask() {
				@Override
				public void run() {
					session.removeAttribute(ConstantIfo.CURRENTEMALLCODE);
				}
			};
			Timer timer = new Timer();
			timer.schedule(task, 180000);
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);
	}

	/**
	 * 注册
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@PostMapping("/reg")
	public ResultVO reg(HttpSession session, MemberInfo mf, String code) throws IOException {
		Object obj = session.getAttribute(ConstantIfo.CURRENTEMALLCODE);
		if(obj == null) {
			return new ResultVO(ResultEnum.CODE_TIMEOUT);
		}
		
		String scode = (String) obj;
		if(!scode.equals(code)) {
			return new ResultVO(ResultEnum.CODE_ERROR);
		}
		
		int result = memberInfoService.reg(mf);
		if(result == -1) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);
	}
	
	/**
	 * 登录
	 * @param session
	 * @param map
	 * @param code
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/login")
	public ResultVO login(HttpSession session, @RequestParam Map<String, Object> map, String code) throws IOException {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINCODE);//获取验证码
		String scode = (String) obj;
		if(!scode.equalsIgnoreCase(code)) {
			return new ResultVO(ResultEnum.CODE_ERROR);//验证码错误
		}
		
		MemberInfo mf = memberInfoService.login(map);
		if(mf == null) {
			return new ResultVO(ResultEnum.ERROP);//校验错误
		}
		
		session.setAttribute(ConstantIfo.CURRENTLOGINMEMBER, mf);
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	/**
	 * 获取当前登录的用户信息
	 */
	@GetMapping("/info")
	public ResultVO getInfo(HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS, obj);	
	}
	
}
